<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

/**
 * 商品属性规格模型
 * Class ZhunleGoodsAttr
 * @package App\Models\CacheModel
 */
class MallGoodsAttr extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.mall_goods_attr';
    protected $cacheModel = 'goods.mall_goods_attr';

    /**
     * 更新规格缓存
     * @param $id
     * @throws \Exception
     */
    public function UpdateCache($id)
    {
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
    }

    /**
     * 获取指定商品的规格属性
     * @param $gid
     * @param $field
     * @return array|mixed
     * @throws \Exception
     */
    public function getGoodsAttrList($gid, $field = [])
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $gid)->setSort('DESC')->ZsGetIocData('list', function () use ($gid)
        {
            $where = ['status' => 1, 'gid' => $gid];
            try
            {
                $ids = $this->where($where)->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $ids = [];
            }
            return $ids;
        });
        $data = $this->batGetInfo($ids, $field);

        $attrData = [];
        foreach ($ids as $id)
        {
            $attrData[] = $data[$id];
        }

        return $attrData;
    }

    /**
     * 获取规格属性信息
     * @param $ids
     * @param $field
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function batGetInfo($ids, $field = [])
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel)->batchGetCache($ids, function ($ids)
        {
            try
            {
                $data = $this->where(['id' => ['in', $ids]])->column('id,sort,name,description,img,or_price,now_price,vip_price,discount,inventory,attr,gid,status', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, $field);
    }
}
